<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <!-- load 依赖的库 -->
    <script src="../libs/system.js"></script>
    <script>
        System.import('../libs/cc.js');
    </script>

    <!-- load 一些工具函数 -->
    <script src="../src/utils/gltfParser.js"></script>
    <script src="../src/utils/ajax.js"></script>

    <!-- load需要的 -->
    <script src="../src/debugUtils.js"></script>
    <script src="../src/graphics.js"></script>
    <script src="../src/v1/camera.js"></script>
    <script src="../src/v1/rendererV1.js"></script>
    <script src="../src/v1/mesh.js"></script>
</head>
<body>

</body>
<script>

    window.onload = function () {

        //



        // ajax({
        //     url:'./assets/Love.gltf'
        // }).then((content)=>{
        //     console.log('content:',content);
        //     start(content);
        // }).catch((err)=>{
        //     console.log('err:',err);
        // });


    };

    function start(content) {
        console.log(typeof content);
        let parser = new GltfParser(content);
        console.log(parser.gltf);

        let primitives = parser.getPrimitives();
        if(primitives.length > 0){
            // first
            let p = primitives[0];
            let mesh = getVertexs(p);
            console.log('--------------------------------');
            console.log('mesh:',mesh);
        }
    }

    function getVertexs(primitive) {

        //  验证三角形环绕方式
        if(primitive.mode !== 4){
            /**
             * 0: POINTS
             * 1: LINES
             * 2: LINE_LOOP
             * 3: LINE_STRIP
             * 4: TRIANGLES
             * 5:TRIANGLE_STRIP
             * 6:TRIANGLE_FAN
             */
            throw new Error('暂只支持 TRIANGLES!');
        }

        // 索引信息
        let indices = primitive.indices.srcData;

        // 所有的顶点信息
        // 顶点信息
        let attrs = primitive.attributes;
        let vInfo = attrs[0];
        // 验证类型 (FLOAT)
        if(vInfo.componentType !== 5126){
            throw new Error('暂不支持其他类型!');
        }

        let vertexs = vInfo.srcData; // float array


        // TODO:
        // 其余的信息, 如:
        // uv  normal  material 等后面处理

        //  还包含aabb
        let min = vInfo.min;
        let max = vInfo.max;
        return {
            vertexs,
            indices,
            min,
            max,
        }
    }

</script>
</html>
